#include <QPainter>

#include "colorBar.h"

colorBar::colorBar(float aMin, float aMax, QWidget* parent) :
        QWidget(parent), min(aMin), max(aMax)
{
    //setPalette(QPalette(QColor(200, 200, 200)));
    //setAutoFillBackground(true);
}

void colorBar::reload(float aMin, float aMax){
    min=aMin;
    max=aMax;
    update();
}

QImage colorBar::image()
{
    update();
    QImage image(this->size(),  QImage::Format_ARGB32);
    image.fill(qRgb(255,255,255));
    if (!image.isNull()){
        QPainter painter(&image);
        doPaint(painter);
        painter.end();
    }
    return image;
}

void colorBar::paintEvent(QPaintEvent* /*event*/)
{
    QPainter painter(this);
    doPaint(painter);

}
void colorBar::doPaint(QPainter& painter)
{
    //TODO: get color scheme (don't generate it here) from colorScale, and also get min and max there
    for (int i=0; i<=this->height(); i++){
        QColor color("black");
        float coef=(float) i/this->height();   //number between 0 and 1
        color.setRedF(1-coef);
        color.setBlueF(coef);
        painter.setPen(color);
        painter.drawLine(0, i, this->width(), i);
    }
    int textHeight=10;
    int margin=3;
    painter.setPen(QColor("white"));
    painter.drawText(margin, textHeight , QString::number(max) );
    painter.drawText(margin, this->height()-margin, QString::number(min) );
}
